// pages/public/forget_password/forget_password.js
const http = require("../../../utils/httpUtils.js");
const RSA = require("../../../utils/wx_rsa.js");
const app = getApp();
Page({
    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '密码设置' //导航栏 中间的标题
        },
        height: app.globalData.height * 2 + 20,
        send: true,
        second: 60
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },
    formSubmit(e) { //提交修改密码
        // console.log('form发生了submit事件，携带数据为：', e.detail.value)
        if (this.data.phone == undefined) {
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                mask: true
            })
            return
        }
        if (this.data.phone.length != 11) {
            wx.showToast({
                title: '手机号格式错误',
                icon: 'none'
            })
            return
        }
        let formData = e.detail.value;
        if (formData.password != formData.passwordConfirm) {
            wx.showToast({
                title: '两次输入密码不一致',
                icon: 'none'
            })
            return
        }
        let data = {
            phone: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(formData.phone)),
            verify_code: formData.yzm,
            client: 'weixinSmall',
            newPassword: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(formData.passwordConfirm))
        }
        http._post('/api/user/forget_password', data, res => {
            console.log(res.data)
            if (res.data.code == 1) {
                wx.showModal({
                    title: '提示',
                    content: '密码修改成功,请牢记新密码',
                    showCancel: false,
                    success: res => {
                        wx.navigateBack({})
                    }
                })
                return
            } else {
                wx.showModal({
                    title: '提示',
                    content: res.data.msg,
                    showCancel: false
                })
                return
            }
        })
    },
    inPhone(e) { //手机号
        this.setData({
            phone: e.detail.value
        })
    },
    yzminput(e) { //验证码
        this.setData({
            verify_code: e.detail.value
        })
    },
    // 发送验证码
    sendMsg() {
        if (this.data.phone == undefined) {
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                mask: true
            })
            return
        }
        if (this.data.phone.length != 11) {
            wx.showToast({
                title: '手机号格式错误',
                icon: 'none'
            })
            return
        }
        let phone = RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(this.data.phone))
        http._get('/api/sendsms/sendsms', {
            phone: phone,
            scene: 'verify_login',
            client: 'weixinSmall'
        }, res => {
            if (res.data.code == 1) {
                wx.showToast({
                    title: '获取成功请稍等',
                    icon: 'none'
                })
                this.setData({
                    send: false
                })
                this.timer()
            } else {
                wx.showModal({
                    title: '提示',
                    content: res.data.msg,
                    showCancel: false
                })
                return
            }
        })
    },
    //倒计时
    timer() {
        let promise = new Promise((resolve, reject) => {
            let setTimer = setInterval(
                () => {
                    this.setData({
                        second: this.data.second - 1
                    })
                    if (this.data.second <= 0) {
                        this.setData({
                            second: 60,
                            send: true
                        })
                        resolve(setTimer)
                    }
                }, 1000)
        })
        promise.then((setTimer) => {
            clearInterval(setTimer)
        })
    }
})